<?php
/**
* @software         [JiJinCMS]
* @author           [上海集锦信息科技有限公司]
* @website          [http://www.jijinweb.com]
* @email            [sunny@jijinweb.com]
**/
namespace app\admin\controller;
use think\facade\View;
use think\facade\Db;
class Index extends \app\admin\controller\Admin{
    public function index()
    {
        //删除过期访问数据
        $year_time=mktime(0, 0, 0, date('m'),date('d'), date('Y')-1);
        $where1["daytime"]=array("LT",$year_time);
        Db::name("visit")->where($where1)->delete();
        $day_time=mktime(0, 0, 0, date('m'),date('d')-2, date('Y'));
        $where2["visit_time"]=array("LT",$day_time);
        Db::name("visit_48")->where($where2)->delete();
        //
        $cat= new \app\admin\controller\Cat($this->app);
        View::assign([
            'Cats'  => $cat->forCatArray(),//栏目菜单
            'Nodes'  => $this->getNodes(),//菜单节点
        ]);          
        //
        return View::fetch();
    }
    //
    public function console()
    {
        //信息数量
        $infoNum=0;
        $tables=Db::name("table")->field("table_name")->select();
        foreach($tables as $v){
            $num=Db::name("cms_".$v['table_name'])->count();
            $infoNum=$num+$infoNum;
        }
        //草稿
        $draftNum=Db::name("cms_cache")->where("state","draft")->count();
        //待审核
        $readyCheckNum=Db::name("cms_cache")->where("state","readyCheck")->count();   
        //待审核
        $fileNum=Db::name("file")->count();
        $feedBackModel =  new \app\admin\model\FeedbackModel();
        $recent_msg = $feedBackModel->order("is_read asc,addtime desc")->limit(10)->select()->toArray();
        foreach($recent_msg as $key=>$v){
            $recent_msg[$key]['cat']=Db::name("feedback_cat")->find($v['catid']);
        }

        $loginModel=new \app\admin\model\UserLoginLogModel();
        $login_log=$loginModel->order("addtime desc")->limit(10)->select()->toArray();;
        View::assign([
            'infoNum'=>$infoNum,
            'draftNum'=>$draftNum,
            'readyCheckNum'=>$readyCheckNum,
            'fileNum'=>$fileNum,
            'recent_msg'=>$recent_msg,
            'login_log'=>$login_log
        ]);
        return View::fetch();
    }


    //菜单
    private function getNodes(){
        $field=array("id","name","title","pid","level","status","icon");
        $and=array();
        $and[]=['level','<',3];
        $and[]=['status','=',1];        
        //非操作管理员只能显示指定节点
        if($this->user_id!=config('app.super_admin_user_id')){
            $NodeIds=$this->getRoleNodeIds()?$this->getRoleNodeIds():[];
            $and[]=['id','in',$NodeIds];
        }
        $data=Db::name('node')->field($field)->where($and)->order('sort asc')->select();
        $data=node_merge($data);
        return $data;
    } 
    //查询当前用户角色拥有的节点
    private function getRoleNodeIds(){
        //根据用户查询节点
        $role_ids=Db::name("role_user")->where(array("user_id"=>$this->user_id))->column("role_id");
        if($role_ids){
            $node_ids=Db::name("access")->where("role_id","in",$role_ids)->column("node_id");  
            return $node_ids;
        }
    }  
    //session刷新，防止在一定时间内不操作，后台登录失效
    public function updateSession(){
        return ;//什么也不用干，返回空就行
    }
    //访问统计
    public function getTongji(){
        $type= request()->param('type');
        //昨天
        if($type=="yesterday"){
            $start_time=strtotime(date("Y-m-d",strtotime("-1 day")));   //昨天开始时间
            $end_time=strtotime(date("Y-m-d"))-1;  //昨天结束时间也是今天开始时间-1
            //0-23点
            for($i=0;$i<=23;$i++){
                //名称
                $for["bar_name"][]=$i;
                $start_time2=strtotime(date("Y-m-d",strtotime("-1 day"))." ".$i.":00:00");
                $end_time2=strtotime(date("Y-m-d",strtotime("-1 day"))." ".$i.":59:59");
                //IP统计
                $where=array();
                $where[]=array("visit_time","between",array($start_time2,$end_time2));
                $ip_total=Db::name("visit_48")->where($where)->group("ip")->select();
                $for["bar_ip_total"][]=count($ip_total);
                //PV统计
                $for["bar_pv_total"][]=Db::name("visit_48")->where($where)->count();

            }
            //总计统计
            $alltotal=Db::name("visit")->where(array("daytime"=>$start_time))->find();

        }
        //最近7天的
        else if($type=="week"){
            $start_time=strtotime(date("Y-m-d",strtotime("-7 day")));   //开始时间
            $end_time=time();  //结束时间
            //
            for($i=6;$i>=0;$i--){
                //名称
                $for["bar_name"][]=date("m-d",strtotime("-".$i." day"));
                $daytime=strtotime(date("Y-m-d",strtotime("-".$i." day")));
                //IP统计
                $where=array();
                $where["daytime"]=$daytime;
                $total=Db::name("visit")->where($where)->find();
                $for["bar_ip_total"][]=(int)$total['ip'];
                //PV统计
                $for["bar_pv_total"][]=(int)$total['pv'];

            }
            $where = array();
            $where[] = ['daytime', 'between', [$start_time, $end_time]];
            $total = Db::name('visit')->field('SUM(ip)as ip,SUM(pv)as pv,SUM(visit_search)as visit_search,SUM(visit_link)as visit_link,SUM(visit_website)as visit_website,SUM(engine_baidu)as engine_baidu,SUM(engine_google)as engine_google,SUM(engine_sogou)as engine_sogou,SUM(engine_360)as engine_360')->where($where)->select();
            //总计统计
            $alltotal=$total[0];
        }
        //最近30天的
        else if($type=="month"){
            $start_time=strtotime(date("Y-m-d",strtotime("-30 day")));   //开始时间
            $end_time=time();  //结束时间
            //
            for($i=29;$i>=0;$i--){
                //名称
                $for["bar_name"][]=date("m-d",strtotime("-".$i." day"));
                $daytime=strtotime(date("Y-m-d",strtotime("-".$i." day")));
                //IP统计
                $where=array();
                $where["daytime"]=$daytime;
                $total=Db::name("visit")->where($where)->find();
                $for["bar_ip_total"][]=(int)$total['ip'];
                //PV统计
                $for["bar_pv_total"][]=(int)$total['pv'];

            }

            $where = array();
            $where[] = ['daytime', 'between', [$start_time, $end_time]];
            $total = Db::name('visit')->field('SUM(ip)as ip,SUM(pv)as pv,SUM(visit_search)as visit_search,SUM(visit_link)as visit_link,SUM(visit_website)as visit_website,SUM(engine_baidu)as engine_baidu,SUM(engine_google)as engine_google,SUM(engine_sogou)as engine_sogou,SUM(engine_360)as engine_360')->where($where)->select();
            //总计统计
            $alltotal=$total[0];
        }
        //最近半年
        else if($type=="halfyear"){
            $start_time=mktime(0, 0, 0,date('m')-5,1, date('Y'));   //开始时间
            $end_time=time();  //结束时间
            //
            for($i=5;$i>=0;$i--){
                //名称
                $for["bar_name"][]=date("Y-m",mktime(0, 0, 0, date('m')-$i,1, date('Y')));
                $start_time2=mktime(0, 0, 0, date('m')-$i,1, date('Y'));
                $end_time2=mktime(0, 0, 0, date('m')-($i-1),0, date('Y'));
                $where = array();
                $where[] = ['daytime', 'between', [$start_time2, $end_time2]];
                $total=Db::query("select SUM(ip)as ip,SUM(pv)as pv from ".config('database.connections.mysql.prefix')."visit where daytime between ".$start_time2." and ".$end_time2." limit 1");
                $for["bar_ip_total"][]=(int)$total[0]['ip'];
                //PV统计
                $for["bar_pv_total"][]=(int)$total[0]['pv'];

            }
            $where = array();
            $where[] = ['daytime', 'between', [$start_time, $end_time]];
            $total = Db::name('visit')->field('SUM(ip)as ip,SUM(pv)as pv,SUM(visit_search)as visit_search,SUM(visit_link)as visit_link,SUM(visit_website)as visit_website,SUM(engine_baidu)as engine_baidu,SUM(engine_google)as engine_google,SUM(engine_sogou)as engine_sogou,SUM(engine_360)as engine_360')->where($where)->select();
            //总计统计
            $alltotal=$total[0];
        }
        //最近一年
        else if($type=="year"){
            $start_time=mktime(0, 0, 0,date('m')-11,1, date('Y'));   //开始时间
            $end_time=time();  //结束时间
            //
            for($i=11;$i>=0;$i--){
                //名称
                $for["bar_name"][]=date("Y/m",mktime(0, 0, 0, date('m')-$i,1, date('Y')));
                $start_time2=mktime(0, 0, 0, date('m')-$i,1, date('Y'));
                $end_time2=mktime(0, 0, 0, date('m')-($i-1),0, date('Y'));
                $where = array();
                $where[] = ['daytime', 'between', [$start_time2, $end_time2]];
                $total=Db::query("select SUM(ip)as ip,SUM(pv)as pv from ".config('database.connections.mysql.prefix')."visit where daytime between ".$start_time2." and ".$end_time2." limit 1");

                $for["bar_ip_total"][]=(int)$total[0]['ip'];
                //PV统计
                $for["bar_pv_total"][]=(int)$total[0]['pv'];
            }
            $where = array();
            $where[] = ['daytime', 'between', [$start_time, $end_time]];
            $total = Db::name('visit')->field('SUM(ip)as ip,SUM(pv)as pv,SUM(visit_search)as visit_search,SUM(visit_link)as visit_link,SUM(visit_website)as visit_website,SUM(engine_baidu)as engine_baidu,SUM(engine_google)as engine_google,SUM(engine_sogou)as engine_sogou,SUM(engine_360)as engine_360')->where($where)->select();
            //总计统计
            $alltotal=$total[0];
        }
        //今天
        else{
            $start_time=strtotime(date("Y-m-d")." 00:00:00");
            $end_time=date("Y-m-d")." 23:59:59";

            //0-23点
            for($i=0;$i<=date('H');$i++){
                //名称
                $for["bar_name"][]=$i;
                $start_time2=strtotime(date("Y-m-d")." ".$i.":00:00");
                $end_time2=strtotime(date("Y-m-d")." ".$i.":59:59");     
                //IP统计
                $where=array();
                $where[]=array("visit_time","between",array($start_time2,$end_time2));
                $ip_total=Db::name("visit_48")->where($where)->group("ip")->select();
//                dump($ip_total);exit;
                $for["bar_ip_total"][]=count($ip_total);
                //PV统计
                $for["bar_pv_total"][]=Db::name("visit_48")->where($where)->count();                
            }
            //总计统计
            $alltotal=Db::name("visit")->where(array("daytime"=>$start_time))->find();
        }
        //开始数据统计查询
        $for['pie_name']=array("直达","跳转","百度","谷歌","搜狗","360");
        $for['pie_data1']=array(
            array(
                "name"=>"直达",
                "value"=>(int)$alltotal["visit_website"],
            ),
            array(
                "name"=>"跳转",
                "value"=>(int)$alltotal["visit_link"],
            ),       
            array(
                "name"=>"搜索引擎",
                "value"=>(int)$alltotal["visit_search"],
            ),                   
        );
        $for['pie_data2']=array(
            array(
                "name"=>"直达",
                "value"=>(int)$alltotal["visit_website"],
            ),
            array(
                "name"=>"跳转",
                "value"=>(int)$alltotal["visit_link"],
            ),       
            array(
                "name"=>"百度",
                "value"=>(int)$alltotal["engine_baidu"],
            ),     
            array(
                "name"=>"谷歌",
                "value"=>(int)$alltotal["engine_google"],
            ),  
            array(
                "name"=>"搜狗",
                "value"=>(int)$alltotal["engine_sogou"],
            ),  
            array(
                "name"=>"360",
                "value"=>(int)$alltotal["engine_360"],
            ),                  
        );
        $this->success($for);
    }    
}
